﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
using Microsoft.Samples.Data.Services.Client;
using System.Runtime.Serialization;
using System.Collections.ObjectModel;
using Serialization;

namespace AzureTracer.Common
{
    public class IsolatedStorageHelper
    {
        public static void SaveData(string isoFileName, object value)
        {
            // persist the data using isolated storage
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            using (var stream = new IsolatedStorageFileStream(isoFileName,
                                                            FileMode.Create,
                                                            FileAccess.Write,
                                                            store))
            {
                SilverlightSerializer.Serialize(value, stream);
                //var serializer = new DataContractSerializer(typeof(object));
                //serializer.WriteObject(stream, value);
            }
        }

        public static T LoadData<T>(string isoFileName) where T: class
        {
            object dataRestored = null;
            // load the view model from isolated storage
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            using (var stream = new IsolatedStorageFileStream(isoFileName, FileMode.OpenOrCreate, FileAccess.Read, store))
                try
                {
                    dataRestored = SilverlightSerializer.Deserialize<T>(stream);
                    //var deserializer = new DataContractSerializer(typeof(T));
                    //dataRestored = deserializer.ReadObject(stream);
                }
                catch { }

            return dataRestored as T;
        }

        public static void SaveList<T>(string isoFileName, ObservableCollection<T> dataList) where T : class
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())            
            using (var stream = new IsolatedStorageFileStream(isoFileName, FileMode.Create, store))
            {
                SilverlightSerializer.Serialize(dataList, stream);
                //var serializer = new DataContractSerializer(typeof(ObservableCollection<T>));
                //serializer.WriteObject(stream, dataList);
            }
        }

        public static ObservableCollection<T> LoadList<T>(string dataName) where T : class
        {
            ObservableCollection<T> retval = new ObservableCollection<T>();


            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(dataName, FileMode.OpenOrCreate, store))
                try
                {
                    if (stream.Length > 0)
                    {
                        retval = SilverlightSerializer.Deserialize<ObservableCollection<T>>(stream);
                        //var deserializer = new DataContractSerializer(typeof(ObservableCollection<T>));
                        //retval = deserializer.ReadObject(stream) as ObservableCollection<T>;
                    }
                }
                catch { }

            return retval;
        }

        public static void DeleteFile(string isoFileName)
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                store.DeleteFile(isoFileName);
        }


    }
}
